#include "List.h"
#include <iostream>

using namespace std;

List::List()
{
	m_pList = new Node;
	m_pList->data = 0;
	m_pList->next = NULL;
	m_iLength = 0;
}

List::~List()
{
	ClearList();

	delete m_pList;
	m_pList = NULL;
}

void List::ClearList()
{
	Node *currentNode = m_pList->next;
	while(currentNode != NULL) 
	{
		Node *temp = currentNode->next;
		delete currentNode;

		currentNode = temp;
	}
	m_pList->next = NULL;
	m_iLength = 0;
}

bool List::ListEmpty()
{
	return m_iLength == 0;
}

int List::ListLength()
{
	return m_iLength;
}

bool List::ListInsertHead(Node *pNode)
{
	Node *temp = m_pList->next;
	Node *newNode = new Node;
	newNode->data = pNode->data;

	if (newNode == NULL)
	{
		return false;
	}
	m_pList->next = newNode;
	newNode->next = temp;
	m_iLength++;

	return true;
}

bool List::ListInsertTail(Node *pNode) 
{
	Node *currentNode = m_pList->next;

	while (currentNode != NULL) 
	{
		currentNode = currentNode->next;
	}

	Node *newNode = new Node;
	if (newNode == NULL)
	{
		return false;
	}

	newNode->data = pNode->data;
	newNode->next = NULL;
	currentNode->next = newNode;

	m_iLength++;

	return true;
}

bool List::ListInsert(int index, Node *pNode) 
{
	if (index < 0 || index > m_iLength)
		{
			return false;			
		}	

	Node *currentNode = m_pList->next;
	for (int i = 0; i < index; ++i)
	{
		currentNode = currentNode->next;
	}

	Node *newNode = new Node;
	if (newNode == NULL)
	{
		return false;
	}

	newNode->data = pNode->data;
	newNode->next = currentNode->next;

	currentNode->next = newNode;
	m_iLength++;
	return true;
}

bool List::ListDelete(int index, Node *pNode)
{
	if (index < 0 || index > m_iLength)
	{
		return false;
	}

	Node * currentNode = m_pList->next;
	Node * currentNodeBefore = NULL;
	for (int i = 0; i <= index; ++i)
	{
		currentNodeBefore = currentNode;
		currentNode = currentNode->next;
	}

	currentNodeBefore->next = currentNode->next;
	pNode->data = currentNode->data;
	delete currentNode;
	currentNode = NULL;
	m_iLength--;

	return true;
}

bool List::GetElem(int index, Node *pNode)
{
	if (index < 0 || index > m_iLength)
	{
		return false;
	}

	Node * currentNode = m_pList->next;
	for (int i = 0; i <= index; ++i)
	{
		currentNode = currentNode->next;
	}

	pNode->data = currentNode->data;
	return true;

}

int List::LocateElem(Node *pNode)
{
	Node *currentNode = m_pList->next;
	int index = 0;
	while (currentNode->next != NULL)
	{
		currentNode = currentNode->next;
		if (currentNode->data == pNode->data)
		{
			return index;
		}
		index++;
	}

	return -1;
}

bool List::PriorElem(Node *pCurrentNode, Node *preNode)
{
	Node *currentNode = m_pList->next;
	Node *tempNode = NULL;
	while (currentNode->next != NULL)
	{
		tempNode = currentNode;
		currentNode = currentNode->next;
		if (currentNode->data == pCurrentNode->data)
		{
			if (tempNode == m_pList)
			{
				return false;
			}
			preNode->data = tempNode->data;

			return true;
		}
	}	

	return false;
}

bool List::NextElem(Node *pCurrentNode, Node *nextNode)
{
	Node *currentNode = m_pList->next;
	while (currentNode->next != NULL)
	{
		currentNode = currentNode->next;
		if (currentNode->data == pCurrentNode->data)
		{
			if (currentNode->next == NULL)
			{
				return false;
			}
			nextNode->data = currentNode->next->data;

			return true;
		}
	}	

	return false;	
}

void List::ListTraverse()
{
	Node *currentNode = m_pList->next;
	while(currentNode->next != NULL) 
	{
		currentNode = currentNode->next;
		currentNode->printNode();
	}
}

